package com.njupt.components;

import com.njupt.domain.ManageInfos;
import com.njupt.mapper.ManageInfoMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class MySQLRedis {
    @Resource
    private ManageInfoMapper manageInfoMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Scheduled(cron = "0 49 19 * * ?") // 调度器，每一秒来一次
    public void managesDataTransform(){
        // 读取所有的mysql managerinfos数据 发送到redis数据库的manages hash中
        System.out.println("start");
        List<ManageInfos> mis = manageInfoMapper.findAll();
        for (ManageInfos mi : mis) {
            stringRedisTemplate.opsForHash().put("mananges",mi.getLoginname()+mi.getLoginpwd(),mi.toString());
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("%YY-%mm-%%DD");
//        System.out.println(sdf.format(new Date()));
    }
}
